<template>
  <div
    class="free-edit-container"
    :style="{
      color: themeStyle[theme].textColor1
    }"
  >
    <!-- 画布属性标题 -->
    <div
      class="title"
      :style="{
        borderBottom: `1px solid ${themeStyle[theme].borderColor1}`
      }"
    >
      <span style="color: #0087dc">{{ '节点属性' }}</span>
      <div class="icon" :class="Nodedata.lock ? 'lockIcon' : ''">
        <template v-if="Nodedata.lock">
          <LockOutlined @click="switchLock" />
        </template>
        <template v-else>
          <UnlockOutlined @click="switchLock" />
        </template>
      </div>
    </div>

    <div class="attr-content">
      <SizeTransition>
        <Form
          :model="Nodedata"
          :label-col="{ span: 8 }"
          :wrapper-col="{ span: 16 }"
          :label-align="'left'"
          :disabled="Nodedata.lock"
          autocomplete="off"
        >
          <FormItem>
            <template #label>
              <span
                class="content-item-label"
                :style="{
                  color: themeStyle[theme].textColor1
                }"
                >{{ '边框颜色' }}</span
              >
            </template>
            <div style="display: flex">
              <ColorSelect
                v-model:color="Nodedata.style.borderColor"
                :disable="Nodedata.lock"
                :height="24"
                @change="handleChange"
              />
            </div>
          </FormItem>
          <!-- 背景填充 -->
          <FormItem>
            <template #label>
              <span
                class="content-item-label"
                :style="{
                  color: themeStyle[theme].textColor1
                }"
                >{{ '背景填充' }}</span
              >
            </template>
            <div style="display: flex">
              <ColorSelect
                v-model:color="Nodedata.style.backgroundColor"
                :disable="Nodedata.lock"
                :height="24"
                @change="handleChange"
              />
            </div>
          </FormItem>
        </Form>
      </SizeTransition>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useStyleStore } from '@renderer/store/globalStyle/style'
import { storeToRefs } from 'pinia'
import { Nodedata, handleChange } from './nodeProTemHook'
import { onMounted, onUnmounted, watch } from 'vue'
import SizeTransition from '@renderer/components/sizeTransition/SizeTransition.vue'
import { Form, FormItem } from 'ant-design-vue'
import ColorSelect from '@renderer/components/colorSelect/ColorSelect.vue'

const globalStyleStore = useStyleStore()
const { theme, themeStyle } = storeToRefs(globalStyleStore)

const props = defineProps({
  data: {
    type: Object,
    default: () => ({}),
    required: false
  }
})
Nodedata.value = props.data

watch(
  () => props.data,
  (val) => {
    handleChange()
    Nodedata.value = props.data
  },
  {
    deep: true
  }
)

onMounted(() => {
  window.electron.ipcRenderer.on('canvasData', (e, mainData): void => {
    const { methodName, result } = mainData
    if (methodName === 'moveNode' && result.id === Nodedata.value.id) {
      Nodedata.value.x = result.x
      Nodedata.value.y = result.y
    }
  })
})

const switchLock = (): void => {
  Nodedata.value.lock = !Nodedata.value.lock
  handleChange()
}
</script>

<style scoped lang="scss">
.free-edit-container {
  width: 100%;
  height: 100%;
  font-size: $font-size-medium;

  .title {
    padding: 3px 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: bold;

    .icon {
      margin-right: 10px;
      cursor: pointer;
      width: 25px;
      height: 25px;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 5px;
      color: #7e7e7e;
    }
    .lockIcon {
      background-color: $icon-active-bgColor;
      color: $icon-active-color;
    }
  }

  .attr-content {
    padding: 5px 3px 5px 6px;
    height: calc(100% - 60px);
    user-select: none;
    :deep(.ant-form-item) {
      margin-bottom: 0px;
    }

    :deep(.basic-info-item-prefix) {
      width: 16px;
      font-size: $font-size-small;
    }
    .content-item-label {
      font-size: $font-size-small;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}
</style>
